package com.fuxian.action;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fuxian.model.Notice;
import com.fuxian.service.NoticeService;
import com.fuxian.util.DateUtile;
import com.fuxian.util.ReturnMapUtil;

@Controller
@Scope("prototype")
public class NoticeAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private static final Log log = LogFactory.getLog(NoticeAction.class);
	
	@Resource
	private NoticeService noticeService;
	
	/*============================================*/
	private int notice_id;
	private String title;
	private String author;
	private String content;
	
	private int rows;
	private int page;
	public void setNotice_id(int noticeId) {
		notice_id = noticeId;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	public void setPage(int page) {
		this.page = page;
	}
	
	
	
	/*============================================*/
	
	//未解决
	public String getNoticeList() {
		returnMap = new HashMap<String, Object>();
		page = ((page == 0) ? 1 : page);
		rows = ((rows == 0) ? 1 : rows);
		int totalCount=noticeService.selectCount();
		int max_page = (totalCount + rows - 1) / rows;
		returnMap.put("max_page", max_page);
		returnMap.put("page", page+1);
		returnMap.put("total", totalCount);
		List<Notice> noticeList = noticeService.selectAllNotice(rows * (page - 1),
				rows);
		returnMap.put("rows", noticeList);
		returnMap = ReturnMapUtil.setReturnHeader(returnMap,true, "成功！");
		return SUCCESS;
	}
	public String getNoticeInfo() {
		returnMap = new HashMap<String, Object>();
		Notice notice = noticeService.selectNoticeById(notice_id);
		returnMap.put("title", notice.getTitle());
		returnMap.put("author", notice.getAuthor());
		returnMap.put("create_time", notice.getCreate_time());
		returnMap.put("content", notice.getContent());
		returnMap = ReturnMapUtil.setReturnHeader(returnMap,true, "成功！");
		return SUCCESS;
	}
	public String addNotice() {
		returnMap = new HashMap<String, Object>();
		Notice notice = new Notice();
		notice.setAuthor(author);
		notice.setContent(content);
		notice.setCreate_time(DateUtile.getTimestamp());
		notice.setCreate_user((String) session.get("user_name"));
		notice.setTitle(title);
		noticeService.addNotice(notice);
		returnMap = ReturnMapUtil.setReturnHeader(returnMap,true, "成功！");
		return SUCCESS;
	}
	public String deleteNotice() {
		returnMap = new HashMap<String, Object>();
		noticeService.deleteNoticeById(notice_id);
		returnMap = ReturnMapUtil.setReturnHeader(returnMap,true, "成功！");
		return SUCCESS;
	}
	public String updateNotice() {
		returnMap = new HashMap<String, Object>();
		Notice notice = new Notice();
		notice.setNotice_id(notice_id);
		notice.setAuthor(author);
		notice.setContent(content);
		notice.setCreate_time(DateUtile.getTimestamp());
		notice.setCreate_user((String) session.get("user_name"));
		notice.setTitle(title);
		noticeService.modifyNotice(notice);
		returnMap = ReturnMapUtil.setReturnHeader(returnMap,true, "成功！");
		return SUCCESS;
	}
	
}
